package com.wan.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wan.activity.util.EditTextFocusLintener;
import com.wan.commonsdk.Conet;
import com.wan.commonsdk.HttpUtils;
import com.wan.commonsdk.WanCommonSdk;
import com.wan.commonsdk.WanServerResposeCode;

import java.util.HashMap;
import java.util.regex.Pattern;
//------------------------------------------------------------------------
//
// 绑定手机
//
//------------------------------------------------------------------------

import net.sourceforge.simcpux.Util;

public class mobilebindActive extends BaseActivity implements
		View.OnClickListener {

	public static Activity activity = null;
	private final int REGESTER_SUCCESS = WanServerResposeCode.WAN_REGESTER_SUCCESS;
	private final int BIND_SUCCESS = WanServerResposeCode.WAN_REGESTER_FAILURE;
	private final int NETWORK_ERROR = 10086;
	private Thread mThread;
	private Boolean checkCode = false;
	private int type = 0;
	private Button btn_bind, btn_getcode;
	private TextView mobile_back_btn;
	private EditText mobileNumber, mobileCode;
	private TimeCount time;
	private TextView finish_text;
	private String phoneNumber, code, status;
	private String commend;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (WanCommonSdk.screen_orientation == WanCommonSdk.SCREEN_ORIENTATION_PORTRAIT) {
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		} else {
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onCreate(savedInstanceState);
		setContentView(Util.getResID("mobile_bind", "layout"));
		
		time = new TimeCount(60000, 1000);
		activity = this;
		this.btn_bind = (Button) findViewById(Util.getResID("btn_bind", "id"));
		
		btn_bind.setOnClickListener(this);
		mobile_back_btn = (TextView) findViewById(Util.getResID("mobile_back_btn", "id"));
		
		mobile_back_btn.setOnClickListener(this);

		this.btn_getcode = (Button) findViewById(Util.getResID("btn_getcode", "id"));
		
		btn_getcode.setOnClickListener(this);

		this.finish_text = (TextView) findViewById(Util.getResID("finish_text", "id"));
		
		this.mobileCode = (EditText) findViewById(Util.getResID("mobileCode", "id"));
		
		this.mobileNumber = (EditText) findViewById(Util.getResID("mobileNumber", "id"));
		
		Button mobile_numberedit_close = (Button) findViewById(Util.getResID("mobile_numberedit_close", "id"));
		mobileNumber.setOnFocusChangeListener(new EditTextFocusLintener(mobileNumber, mobile_numberedit_close));
		
	}

	// ------------------------------------------------------------------------
	//
	// 消息处理
	//
	// ------------------------------------------------------------------------
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case REGESTER_SUCCESS:
				hideLoading();
				finish_text.setText("收到验证码后请填写");
				break;
			case BIND_SUCCESS:
				hideLoading();
				Conet.saveuserInfo();
				btn_bind.setVisibility(View.GONE);
				btn_getcode.setVisibility(View.GONE);
				mobileCode.setVisibility(View.GONE);
				mobileNumber.setVisibility(View.GONE);
				finish_text.setText("手机绑定成功");
				break;
			case NETWORK_ERROR:
				hideLoading();
				mThread = null;
				Toast.makeText(getApplicationContext(), "网络出现问题，请重试",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				hideLoading();
				showToast(status);
				mThread = null;
				break;
			}
		}
	};

	// ------------------------------------------------------------------------
	//
	// 线程处理
	//
	// ------------------------------------------------------------------------
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			String userid = Conet.mUserInfo.getUser_id();
			if (type == 1) {

				HashMap<String, String> result = HttpUtils.HttpSendCode(userid,
						"bind", phoneNumber);
				Util.LogUtil("mobilebindActive---1---result:" + result);
				if (result == null) {
					mHandler.obtainMessage(NETWORK_ERROR).sendToTarget();
					return;
				}
				else if (result.get("status").equals(
						String.valueOf(REGESTER_SUCCESS))) {
					mHandler.obtainMessage(REGESTER_SUCCESS, Conet.mUserInfo)
							.sendToTarget();
					finish();
					return;
				} else if (result.get("status").equals(
						String.valueOf(WanServerResposeCode.WAN_HAS_ACCOUNT))) {
					commend = result.get("commend");
				}
				mHandler.obtainMessage(Integer.parseInt(result.get("status")))
						.sendToTarget();
			} else if (type == 2) {

				HashMap<String, String> result = HttpUtils.HttpBindMobile(
						userid, "bind", code, phoneNumber);
				Util.LogUtil("mobilebindActive---2---result:" + result);
				if (result == null) {
					mHandler.obtainMessage(NETWORK_ERROR).sendToTarget();
					return;
				}
				else if (result.get("status").equals(
						String.valueOf(REGESTER_SUCCESS))) {
					//UserInfo userInfo = new UserInfo();
					Conet.mUserInfo.setBind_Phone(phoneNumber);
					mHandler.obtainMessage(BIND_SUCCESS, Conet.mUserInfo)
							.sendToTarget();
					finish();
					return;
				} else if (result.get("status").equals(
						String.valueOf(WanServerResposeCode.WAN_HAS_ACCOUNT))) {
					commend = result.get("commend");
				}
				status = result.get("status").toString();
				mHandler.obtainMessage(Integer.parseInt(result.get("status")))
						.sendToTarget();

			}

		}
	};

	// ------------------------------------------------------------------------
	//
	// 事件处理
	//
	// ------------------------------------------------------------------------
	@Override
	public void onClick(View v) {
		if (v.getId() == Util.getResID("mobile_back_btn", "id")) {
			
			this.finish();
		} else if (v.getId() == Util.getResID("btn_getcode", "id")) {
			
			if (checkPhone()) {
				phoneNumber = this.mobileNumber.getText().toString();

				showLoading(getString(Util.getResID("log_sendcode_hint", "string")));
				
				mThread = new Thread(runnable);
				type = 1;
				time.start();
				mThread.start();

			}
		} else if (v.getId() == Util.getResID("btn_bind", "id")) {
			
			if (checkPhone() && checkCode()) {
				phoneNumber = this.mobileNumber.getText().toString();
				code = this.mobileCode.getText().toString();
				showLoading(getString(Util.getResID("mobile_loading_hint", "string")));
				
				type = 2;
				mThread = new Thread(runnable);
				mThread.start();

			}

		}
	}

	private Boolean checkPhone() {
		int un_length = this.mobileNumber.getText().toString().length();

		Pattern pattern = Pattern.compile("[0-9]*");
		Boolean num = pattern.matcher(this.mobileNumber.getText().toString())
				.matches();
		if (un_length == 0) {
			Toast.makeText(getApplicationContext(),
					this.getString(Util.getResID("mobile_phone_bind", "string")),
					Toast.LENGTH_SHORT).show();
			
			return false;
		}
		if (un_length != 11 && num) {
			Toast.makeText(getApplicationContext(),
					this.getString(Util.getResID("mobile_phone_wrong", "string")),
					Toast.LENGTH_SHORT).show();
			
			return false;
		}

		return true;
	}

	private Boolean checkCode() {
		int un_length = this.mobileCode.getText().toString().length();

		if (un_length <= 0) {
			Toast.makeText(getApplicationContext(),
					this.getString(Util.getResID("check_code_hint", "string")),
					
					Toast.LENGTH_SHORT).show();
			return false;
		}

		return true;
	}

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			btn_getcode.setText("重新验证");

			btn_getcode.setBackgroundResource(Util.getResID("btn_login_se", "drawable"));
			
			btn_getcode.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			btn_getcode.setClickable(false);
			// btn_getcode.setBackgroundColor(0xcccccc);
			btn_getcode.setText(millisUntilFinished / 1000 + "秒");
		}
		
		
	}
}
